$NG.AllReady(function (page, { useValuesChange,useDataIndexChange,useUpdateRows,useUpdateRow,useBeforeClick }) {
    const mstform = $NG.getCmpApi("p_form_ssfill_m")
    const dgrid = $NG.getCmpApi("p_form_ssfill_d")
    let param = parseUrlParams(window.location.href)
    let lockTime = 0
    //获取相关信息
    useBeforeClick(({args})=>{
        if(window._saveLock){
            $NG.alert("当前用工任务书下面存在填报单，请勿重复填报！")
            return false
        }
        return true
    },"save")
    useBeforeClick(({args})=>{
        if(window._saveLock){
            $NG.alert("当前用工任务书下面存在填报单，请勿重复填报！")
            return false
        }
        return true
    },"check")
    if(param.intype == 'click'){
        lockTime=  setInterval(()=>{
            const source = mstform.getItem("u_source_phid").getValue()
            if(mstform.getItem("u_source_phid").getValue()){
                $NG.execServer("getIsOneYgrwstb",{source},(res)=>{
                    if(res.count !== 0){
                        $NG.alert("当前用工任务书下面存在填报单，请勿重复填报！")
                        window._saveLock = true
                    }
                })
                clearInterval(lockTime)
            }
        },1000)
        function compareDates(dateStr1, dateStr2) {
            // 将时间字符串转换为 Date 对象
            const date1 = new Date(dateStr1);
            const date2 = new Date(dateStr2);
        
            // 检查是否成功转换为有效的日期
            if (isNaN(date1.getTime()) || isNaN(date2.getTime())) {
                throw new Error('输入的时间字符串不是有效的日期格式');
            }
        
            // 比较两个日期
            if (date1 < date2) {
                return -1; // date1 早于 date2
            } else if (date1 > date2) {
                return 1; // date1 晚于 date2
            } else {
                return 0; // 两个日期相等
            }
        }
        getYgTaskInfo()
        function getYgTaskInfo(){
            $NG.execServer("getYgTaskInfo",{phid:param.sourceid},(res)=>{
                if(res.count == 0) return 
                const _data = JSON.parse(res.data)
                console.log(_data);
                const form = _data[0].extendObjects
                const dArr = _data.reduce((value,item)=>{
                    value.push(item.extendObjects)
                    return value
                },[])
                $NG.updateState((updater)=>{
                    updater.data.p_form_ssfill_m.setProps({
                        u_name:{
                            label:form.ry_name,
                            value:form.u_name,
                        },
                        phid_org:{
                            label:$NG.getUser().orgName,
                            value:$NG.getUser().orgID,
                        },
                        phid_pc:{
                            label:form.bill_name,
                            value:form.phid_pc
                        },
                        u_employment_type:form.u_employment_type,
                        u_source_phid:form.phid
                    })
                })
                dgrid.clearRows().then(()=>{
                    dgrid.addRows(dArr.reduce((value,item)=>{
                        value.push({
                            u_type:item.u_type,
                            u_score:item.u_score,
                            u_plan_content:item.u_plan_content,
                            u_plan_finishdt:item.u_plan_finishdt
                        })
                        return value
                    },[]))
                })
                
                dgrid.refreshView()
            })
        }
    }

    
	useUpdateRows(({ args }) => {
		console.log(`表格监听`);
		const _d1arr = dgrid.getRows();
		let u_self_score = 0;
		let u_reevaluate_score = 0;
		_d1arr.forEach((element) => {
			u_self_score += element.u_self_score ? element.u_self_score : 0;
			u_reevaluate_score += element.u_reevaluate_score
				? element.u_reevaluate_score
				: 0;
                console.log(`ziping`,(Number(Number(element.u_self_score)) || 0), (Number(element.u_score) || 0));
            if((Number(element.u_score) - Number(Number(element.u_self_score)) || 0) < 0  ){
                element.u_self_score=0
                $NG.alert("自评分大于分值")
            }
		});
		console.log("数组", u_reevaluate_score, u_self_score);
		$NG.updateState((updater) => {
			updater.data.p_form_ssfill_m.setProps({
				u_self_score: u_self_score,
				u_reevaluate_score: u_reevaluate_score,
			});
		});
        
	}, "p_form_ssfill_d");
    useUpdateRow(({args})=>{
        const _d1arr = dgrid.getRows()
        const fpMinute = _d1arr.reduce((value,item)=>{
            value += (Number(item.u_reevaluate_score) || 0)
            return value
        },0)
        if(fpMinute > window._lsz_catch_maxMinute){
            $NG.alert(`复评分不能超过总分, 当前总分：${window._lsz_catch_maxMinute}分`)
            args[0].u_reevaluate_score = 0
        }
        if(compareDates(args[0].u_finish_dt,args[0].u_plan_finishdt) == -1){
            args[0].u_finish_dt = null
            $NG.alert("实际完成时间不能小于计划完成时间")
        }
        dgrid.refreshView()
    },"p_form_ssfill_d")
    useValuesChange(({args})=>{
        getYgstbInfo()
        cntMFilter()
    },"p_form_ssfill_m.phid_pc")
    useValuesChange(({args})=>{
        getYgstbInfo()
    },"p_form_ssfill_m.u_name")
    //查询最大的复评分
    maxMinute()
    function maxMinute(){
        $NG.execServer("getMaxScore",{},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)[0].extendObjects
            window._lsz_catch_maxMinute = (Number(_data.c_name) || 0)
        })
    }
    function getYgstbInfo(){
        const phid_pc = mstform.getItem("phid_pc").getValue()
        const name = mstform.getItem("u_name").getValue()
        $NG.execServer("getYgstbInfo",{phid_pc,name},(res)=>{
            if(res.count == 0) return 
            const _data = JSON.parse(res.data)
            console.log(_data);
            const form = _data[0].extendObjects
            const dArr = _data.reduce((value,item)=>{
                value.push(item.extendObjects)
                return value
            },[])
            $NG.updateState((updater)=>{
                updater.data.p_form_ssfill_m.setProps({
                    u_name:{
                        label:form.ry_name,
                        value:form.u_name,
                    },
                    phid_org:{
                        label:$NG.getUser().orgName,
                        value:$NG.getUser().orgID,
                    },
                    phid_pc:{
                        label:form.bill_name,
                        value:form.phid_pc
                    },
                    u_employment_type:form.u_employment_type,
                    u_source_phid:form.phid
                })
            })
            // dgrid.addRows(dArr.reduce((value,item)=>{
            //     value.push({
            //         u_type:item.u_type,
            //         u_score:item.u_score,
            //         u_plan_content:item.u_plan_content,
            //         u_plan_finishdt:item.u_plan_finishdt
            //     })
            //     return value
            // },[]))
            dgrid.refreshView()
        })
    }

    cntMFilter()
    function cntMFilter() {
		if (!mstform.getItem("phid_pc").getValue()) {
			return;
		}
		$NG.updateUI((updater, state) => {
			updater.fieldSetForm.p_form_ssfill_m.u_name.setProps({
				onBeforeOpen: async () => {
					var phid_pc = $NG
						.getCmpApi("p_form_ssfill_m")
						.getItem("phid_pc")
						.getValue();
					if (!phid_pc) {
						$NG.alert("请先选择项目");
						return false;
					} else {
						return true;
					}
				},
				...(!!$NG
					.getCmpApi("p_form_ssfill_m")
					.getItem("phid_pc")
					.getValue() && {
					clientSqlFilter: {
						xm: $NG
							.getCmpApi("p_form_ssfill_m")
							.getItem("phid_pc")
							.getValue(),
					},
				}),
			});
		});
	}

    function parseUrlParams(url) {
        const paramsObj = {};
        const queryString = url.split('?')[1];
        const paramPairs = queryString.split('&');
        paramPairs.forEach((paramPair) => {
            const [key, value] = paramPair.split('=');
            const decodedValue = decodeURIComponent(value);
            paramsObj[key] = decodedValue;
        });
        return paramsObj;
    }

})